package com.itestor.iptv.modules.common.actions;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.domain.RoleGroup;
import com.itestor.iptv.modules.common.service.RoleGroupService;
import com.itestor.iptv.modules.common.utils.CommonConstant;


/**
 * 
 * @author Mr.li
 * 
 */
public class RoleGroupNaviAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(RoleGroupNaviAction.class);

    private RoleGroupService roleGroupService;
    
    /**
     * 添加权限组
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {       
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_ADD);
    }

    /**
     * 修改权限组
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        RoleGroup group = roleGroupService.get(Integer.parseInt(id));
        
        if (group == null) {
            return null;
        }
        LoggerUtils.logActionInfo("To modify the group " + group + ".",
                logger, request);
        
        request.setAttribute(CommonConstant.REQUEST_ATTR_ROLEGROUP, group);        
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_MODIFY);        
    }

    /**
     * 分配设备
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward addRelRobot(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String roleGroupId = request.getParameter("queryIdRoleGroup");
        String robotIds = request.getParameter("robotIds");
        
        logger.info("addRelRobot roleGroupId :" +roleGroupId +"  robotIds :"+robotIds);        
        if (StringUtils.isEmpty(roleGroupId)) {
            return null;
        }
        
        String [] ids=robotIds.split(",");        
        List<Integer> robotIdList=new ArrayList<Integer>();
        
        for(String id : ids){
            robotIdList.add(Integer.parseInt(id));
        }
        
        //分配设备
        roleGroupService.addRelRobot(Integer.parseInt(roleGroupId),robotIdList);    
        
        request.setAttribute("queryIdRoleGroup", roleGroupId);        
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_ADDROBOT);
    }

    
    /**
     * 删除分组设备
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward deleteRelRobot(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String roleGroupId = request.getParameter("roleGroupId");
        
        if (StringUtils.isEmpty(roleGroupId)) {
            return null;
        }
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        
        if (null != pvids && pvids.length > 0) {            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("Delete the GroupManage - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            roleGroupService.deleteRelRobot(roleGroupId,ids);
        }
                
        request.setAttribute("queryIdRoleGroup", roleGroupId);        
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_DELROBOT);        
    }
    
    /**
     * 授权设备浏览权限
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward grantBrowse(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String idRoleGroup = request.getParameter("idRoleGroup");
        
        if (StringUtils.isEmpty(idRoleGroup)) {
            return null;
        }
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        
        if (null != pvids && pvids.length > 0) {            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("grant browse the GroupManage - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            roleGroupService.grantBrowseRelRobot(idRoleGroup,ids);
        }
                
        request.setAttribute("queryIdRoleGroup", idRoleGroup);
        
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_GRANTBROWSE);        
    }
    
    /**
     * 授权设备操作权限
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward grantExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String idRoleGroup = request.getParameter("idRoleGroup");
        
        if (StringUtils.isEmpty(idRoleGroup)) {
            return null;
        }
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        
        if (null != pvids && pvids.length > 0) {            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("grant execute the GroupManage - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            roleGroupService.grantExecuteRelRobot(idRoleGroup,ids);
        }
                
        request.setAttribute("queryIdRoleGroup", idRoleGroup); 
        
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_GRANTEXECUTE);        
    }
    
    /**
     * 删除权限组
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {
            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            
            LoggerUtils.logActionInfo("Delete the RoleGroup - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            roleGroupService.delete(ids);
        }
        
        return mapping.findForward(CommonConstant.FORWARD_ROLEGROUP_DELETE);
    }

    /**
     * 
     * @param pRoleGroupService
     */
    public void setRoleGroupService(RoleGroupService pRoleGroupService) {
        roleGroupService = pRoleGroupService;
    }
    
}
